package com.bjpowernode.boss;

import com.bjpowernode.boss.mapper.DicPayTypeMapper;
import com.bjpowernode.boss.mapper.DicPayWayMapper;
import com.bjpowernode.boss.mvc.model.dto.PayTypeGroupContentDto;
import com.bjpowernode.boss.mvc.model.vo.DitOptionVo;
import jakarta.annotation.Resource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 实现初始化的接口
 * ApplicationRunner
 * CommandLineRunner
 * 区别
 * CommandLineRunner run(String... args)
 * ApplicationRunner run(ApplicationArguments args)
 */
@SpringBootApplication
@MapperScan("com.bjpowernode.boss.mapper")
@EnableAsync  // 添加异步支持
public class PowerBossApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(PowerBossApplication.class, args);
    }

    @Resource
    private ApplicationContext applicationContext;

    public static List<DitOptionVo> DIC_PAY_WAY_LIST;

    public static Map<String, List<DitOptionVo>> DIC_PAY_TYPE_MAP = new HashMap<>();

    // 定义两个map集合 , 用来通过 编码查询 对应name值
    public static Map<String, String> DIC_PAY_TYPE_NAME_MAP;
    public static Map<String ,String> DIC_PAY_WAY_NAME_MAP;
    /**
     * run方法 , 在spring容器初始化完成后执行的
     * run方法执行时 , 对象已经创建好了  ,  spring容器也创建好了
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        // 读取dic_pay_way表的数据 , 缓存到  内存中
        DicPayWayMapper payWayMapper = applicationContext.getBean(DicPayWayMapper.class);
        DIC_PAY_WAY_LIST = payWayMapper.selectDicList();
        DIC_PAY_WAY_NAME_MAP = DIC_PAY_WAY_LIST.stream().collect(Collectors.toMap(DitOptionVo::getCode, DitOptionVo::getLabel));


        // 读取dic_pay_type表的数据 ,
        DicPayTypeMapper dicPayTypeMapper = applicationContext.getBean(DicPayTypeMapper.class);
        // 根据pay_way_code 进行分组 , 将分组数据进行 group_concat 拼接
        List<PayTypeGroupContentDto> payTypeGroupContentDtoList = dicPayTypeMapper.selectTypeDicByGroupConcatList();

        // 遍历查询到的 结果
        DIC_PAY_TYPE_NAME_MAP = new HashMap<>();
        for (PayTypeGroupContentDto payTypeGroupContentDto : payTypeGroupContentDtoList) {
            // 定义 模型集合
            List<DitOptionVo> payTypeItemList = new ArrayList<>();
            // 切割分段
            String[] groupItem = payTypeGroupContentDto.getContent().split("#");
            // 遍历
            for (String item : groupItem) {
                String[] payTypeItem = item.split(":");
                DitOptionVo ditOptionVo = new DitOptionVo();
                ditOptionVo.setCode(payTypeItem[0]);
                ditOptionVo.setLabel(payTypeItem[1]);
                payTypeItemList.add(ditOptionVo);
                DIC_PAY_TYPE_NAME_MAP.put(payTypeGroupContentDto.getPayWayCode() + ":" + payTypeItem[0], payTypeItem[1]);
            }
            DIC_PAY_TYPE_MAP.put(payTypeGroupContentDto.getPayWayCode(), payTypeItemList);
        }
    }
}
